package com.situ.day3;

import org.junit.Test;

public class ArrayDemo {
    @Test
    public void test1() {
        int score1 = 3;
        int score2 = 3;
        int score3 = 3;
        // 数组：存放同一种类型数据的集合，在内存里面是开辟一块连续的区域。
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        for (int i = 0; i < array.length; i++) {
            //System.out.println(i);
            System.out.println(array[i]);
        }
        System.out.println("-------------------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test2() {
        int[] array1 = {34, 45, 7, 6};
        int[] array2 = new int[]{34, 45, 7, 6};
        System.out.println("array1: " + array1.length);
        System.out.println("array2: " + array2.length);
    }

    //只要能遍历所有元素：求最大值、最小值、排序。
    @Test
    public void test3() {
        int[] array = {4, 17, 21, 1};
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            //System.out.println(array[i]);
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);
    }

    @Test
    public void test32() {
        int[] array = {4, 17, 21, 1};
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
            //System.out.println(array[i]);
            if (array[i] < min) {
                min = array[i];
            }
        }
        System.out.println("min: " + min);
    }

    @Test
    public void test34() {
        int[] array = {4, 17, 21, 1};
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        System.out.println("sum: " + sum);
    }

    @Test
    public void test45() {
        int num1 = 3;
        int num2 = 5;
        int temp = num1;
        num1 = num2;
        num2 = temp;
        System.out.println("num1: " + num1);
        System.out.println("num2: " + num2);
    }

    @Test
    public void test65() {
        //i=1    j=3
        //i=2    j=2
        //i=3    j=1
        //i+j=4=array.length
        int[] array = {40, 17, 21, 1};
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

}
